package com.bookstore;

import com.bookstore.db.BookStoreDao;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.SimpleCursorAdapter;

public class ViewListCartActivity extends MenuListActivity {
	/** Called when the activity is first created. */
	private static final int ACTIVITY_COMPRAR = 4;

	private BookStoreDao dao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (dao == null) {
			dao = new BookStoreDao(this);
			dao.open();
		}
		setContentView(R.layout.view_list_cart);
		
		if (BookStoreActivity.usuario == null) {
			Intent i = new Intent(ViewListCartActivity.this, BuyActivity.class);
			startActivityForResult(i, ACTIVITY_COMPRAR);
		} else {			
			loadCarts();
		}
	}

	private void loadCarts() {

		Cursor c;

		try {
			c = dao.fetchComprasUsuario(BookStoreActivity.usuario.getUsuario());
			startManagingCursor(c);
			String[] from = new String[] { "fecha", "total" };
			int[] to = new int[] { R.id.fecha, R.id.total };
			SimpleCursorAdapter list = new SimpleCursorAdapter(this,
					R.layout.view_list_cart_row, c, from, to);
			setListAdapter(list);

		} catch (Exception e) {
			Log.e("loadCarts", "Error cargando listView de las compras");
		}
	}
}
